<template>
  <div class="profile-container">
    <!-- 未登录 -->
    <div @click="$router.push('/login')" v-if="!user" class="not-login heaber-bj">
      <img src="@/assets/mobile.png">
      <span>登录 / 注册</span>
    </div>

    <!-- 已登录 -->
    <div v-else class="login-box heaber-bj">
      <div class="user-info">
        <div class="left">
          <van-image
            round
            fit="cover"
            :src="users.photo"
          />
          <span> {{ users.name }} </span>
        </div>
        <van-button type="default" to="/user">编辑资料</van-button>
      </div>
      <div class="user-data">
        <div class="data-item">
          <span> {{ users.art_count }} </span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span> {{ users.follow_count }} </span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span> {{ users.fans_count }} </span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span> {{ users.like_count }} </span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <!-- 收藏/历史 -->
    <van-grid clickable :column-num="2">
      <van-grid-item icon="home-o" text="收藏" to="/shoucang">
        <template #icon>
          <i class="toutiao toutiao-shoucang"></i>
        </template>
      </van-grid-item>
      <van-grid-item icon="search" text="历史" to="/lishi">
        <template #icon>
          <i class="toutiao toutiao-lishi"></i>
        </template>
      </van-grid-item>
    </van-grid>

    <div class="marginBox">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <van-cell v-if="user" @click="onLogout" class="logout-btn" title="退出" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog, Toast } from 'vant'
import { getUserInfo } from '@/api/user.js'

export default {
  data () {
    return {
      users: {}
    }
  },

  computed: {
    ...mapState(['user'])
  },

  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },

  methods: {
    onLogout () {
      Dialog.confirm({
        title: '黑马头条',
        message: '确认退出?'
      }).then(() => {
        this.$store.commit('setuser', null)
      }).catch(() => {})
    },
    async loadUserInfo () {
      try {
        const res = await getUserInfo()
        this.users = res
      } catch (err) {
        Toast('系统异常')
      }
    }
  }
}
</script>

<style scoped lang="less">
.profile-container{
  .heaber-bj{
    width: 100%;
    height: 401px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  .not-login{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img{
      width: 132px;
      height: 132px;
    }
    span{
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
  .login-box{
    overflow: hidden;
    .user-info{
      margin-top: 116px;
      height: 132px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 32px;
      .left{
        display: flex;
        align-items: center;
        .van-image{
          width: 132px;
          height: 132px;
        }
        span{
          font-size: 29px;
          color: #fff;
          margin-left: 22px;
        }
      }
      .van-button{
        width: 115px;
        height: 32px;
        font-size: 20px;
        color: #666;
        border-radius: 16px;
        padding: 0;
      }
    }
    .user-data{
      height: 153px;
      padding: 0 32px;
      display: flex;
      .data-item{
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: #fff;
        span:nth-child(1){
          font-size: 26px;
        }
        span:nth-child(2){
          font-size: 23px;
          margin-top: 14px;
        }
      }
    }
  }
  .van-grid{
    .toutiao{
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #EB5253;
    }
    .toutiao-lishi {
      color: #FF9D1D;
    }
    /deep/.van-grid-item__text{
      font-size: 27px;
      color: #333;
      margin-top: 10px;
      letter-spacing: 15px;
      padding-left: 15px;
    }
  }
  .marginBox{
    margin: 10px 0;
  }
  .logout-btn{
    font-size: 29px;
    color: #d86262;
    text-align: center;
  }
}
</style>
